-- || Created by Revelse

Room = inherit(Object)

function Room:constructor(gamemode)
	self.m_Player = {}
	self.m_Team = {}
	self.m_Gamemode = gamemode
	self.m_MaxPlayers = 0
	self.m_MinPlayers = 0
	self.m_RoomID = #gamemode.m_Rooms+1
	self.m_Map = MapManager:getSingleton():getMapsByTag("ffa")[1]
	self.m_NextMap = false
	self.m_Dimension = math.random(65535) -- Change it later
end

function Room:isVoteEnabled () 
	if self.voteSystemActive == nil then
		return false
	end
	return true
end

function Room:getDimension  () return self.m_Dimension    end
function Room:setMaxPlayers (int) self.m_MaxPlayers = int end
function Room:setMinPlayers (int) self.m_MinPlayers = int end
function Room:getMaxPlayers () return self.m_MaxPlayers   end
function Room:getMinPlayers () return self.m_MinPlayers   end
function Room:getPlayers    () return self.m_Player       end

function Room:getCurrentMap () return self.m_Map 			   end
function Room:setNextMap    (mapName) self.m_NextMap = mapName end

function Room:isPlayerInRoom(player)
	for key, value in ipairs(self.m_Player) do
		if value == player then
			return true
		end
	end
	return false
end 

function Room:setPlayerTeam(player,team)
	self.m_Team[player] = team
end

function Room:getPlayerTeam(player)
	return self.m_Team[player]
end

function Room:removePlayer(player)
	for key, value in ipairs ( self.m_Player ) do
		if value == player then
			table.remove(self.m_Player,key)
			self.m_Team[player] = nil
			
			player.currentRoomID   = nil
			player.currentGamemode = nil
			
			player:triggerEvent("showLobbyChoose")
			player:triggerEvent("deactivateVoteSystem")
			
			if self.onLeave then self:onLeave(player) end
			
			return true
		end
	end
	return false
end

function Room:addPlayer (player)
	if player.currentGamemode then
		player:sendMessage("[%s] Du befindest dich bereits in einem Raum!",255,0,0,player:getGamemode():getName())
		player:sendMessage("[%s] Verwende /leave um diesen Raum zu verlassen!",255,0,0,player:getGamemode():getName())
		return false
	end
	if #self:getPlayers()+1 > self:getMaxPlayers() then
		player:sendMessage("[%s] Der von Ihnen gewuenschte Raum ist voll!",255,0,0,self.m_Gamemode:getName())
		return false
	end
	
	player.currentRoomID   = self.m_RoomID
	player.currentGamemode = self.m_Gamemode
	
	player:triggerEvent("hideLobbyChoose")
	
	table.insert(self.m_Player, player)
	
	if self.onJoin then self:onJoin(player) end
	
	return true
end